﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

public class DbObject
{
	// Methods
	public DbObject(string newConnectionString)
	{
		this.m_connectionString = newConnectionString;
		this.Connection = new SqlConnection(this.m_connectionString);
	}

	private SqlCommand BuildIntCommand(string storedProcName, IDataParameter[] parameters)
	{
		SqlCommand command = this.BuildQueryCommand(storedProcName, parameters);
		command.Parameters.Add(new SqlParameter("ReturnValue ", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
		return command;
	}

	private SqlCommand BuildQueryCommand(string storedProcName, IDataParameter[] parameters)
	{
		SqlCommand command = new SqlCommand(storedProcName, this.Connection) { CommandType = CommandType.StoredProcedure };
		SqlParameter parameter = null;
		foreach (SqlParameter parameter_loopVariable in parameters) {
			parameter = parameter_loopVariable;
			command.Parameters.Add(parameter);
		}
		return command;
	}

	private string BuildWhereText(IDataParameter[] parameters)
	{
		string whereText = " WHERE ";
		if ((parameters.Length > 0)) {
			SqlParameter parameter = null;
			foreach (SqlParameter parameter_loopVariable in parameters) {
				parameter = parameter_loopVariable;
				whereText = string.Concat(new string[] {
					whereText,
					parameter.ParameterName,
					" = '",
					parameter.Value.ToString(),
					"' "
				});
			}
			return whereText;
		}
		return "";
	}

	internal SqlDataReader GetEntireView(string storedProcName)
	{
		this.Connection.Open();
		SqlCommand command = new SqlCommand("SELECT * FROM [XunPan].[dbo].[vw_XPInquiryEvent]", this.Connection) { CommandType = CommandType.StoredProcedure };
		return command.ExecuteReader(CommandBehavior.CloseConnection);
	}

	internal DataSet GetEntireView(string viewName, string tableName)
	{
		DataSet dataSet = new DataSet();
		this.Connection.Open();
		SqlDataAdapter sqlDA = new SqlDataAdapter();
		SqlCommand command = new SqlCommand("SELECT * FROM [XunPan].[dbo].[vw_XPInquiryEvent]", this.Connection);
		sqlDA.Fill(dataSet, tableName);
		this.Connection.Close();
		return dataSet;
	}

	internal SqlDataReader GetViewByParameters(string storedProcName, IDataParameter[] parameters)
	{
		this.Connection.Open();
		SqlCommand command = new SqlCommand(("SELECT * FROM [XunPan].[dbo].[vw_XPInquiryEvent]" + this.BuildWhereText(parameters)), this.Connection) { CommandType = CommandType.StoredProcedure };
		return command.ExecuteReader(CommandBehavior.CloseConnection);
	}

	internal DataSet GetViewByParameters(string storedProcName, IDataParameter[] parameters, string tableName)
	{
		DataSet dataSet = new DataSet();
		this.Connection.Open();
		SqlDataAdapter sqlDA = new SqlDataAdapter();
		SqlCommand command = new SqlCommand(("SELECT * FROM [XunPan].[dbo].[vw_XPInquiryEvent]" + this.BuildWhereText(parameters)), this.Connection);
		sqlDA.Fill(dataSet, tableName);
		this.Connection.Close();
		return dataSet;
	}

	internal SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
	{
		this.Connection.Open();
		SqlCommand command = this.BuildQueryCommand(storedProcName, parameters);
		command.CommandType = CommandType.StoredProcedure;
		return command.ExecuteReader(CommandBehavior.CloseConnection);
	}

	internal DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
	{
		DataSet dataSet = new DataSet();
		this.Connection.Open();

        new SqlDataAdapter { SelectCommand = this.BuildQueryCommand(storedProcName, parameters) }.Fill(dataSet, tableName);

        //New SqlDataAdapter() With { _
        //    .SelectCommand = Me.BuildQueryCommand(storedProcName, parameters) _
        //}.Fill(dataSet, tableName)
		this.Connection.Close();
		return dataSet;
	}

	internal int RunProcedure(string storedProcName, IDataParameter[] parameters,  int rowsAffected)
	{
		this.Connection.Open();
		SqlCommand command = this.BuildIntCommand(storedProcName, parameters);
		rowsAffected = command.ExecuteNonQuery();
		int result = Convert.ToInt32(command.Parameters["ReturnValue "].Value);
		this.Connection.Close();
		return result;
	}

	internal void RunProcedure(string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName)
	{
		this.Connection.Open();
		//New SqlDataAdapter() With { _
		//    .SelectCommand = Me.BuildIntCommand(storedProcName, parameters) _
		//}.Fill(dataSet, tableName)
		this.Connection.Close();
	}


	// Properties
	protected string ConnectionString {
		get { return this.m_connectionString; }
	}


	// Fields
	protected SqlConnection Connection;
	private string m_connectionString;
}
